package UI;

import entity.User;
import util.JdbcDeal;
import controller.worker.Worker_add;
import controller.worker.Worker_update;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UIworker extends JPanel implements ActionListener {
    User user;
    JPanel p1,p2;
    JComboBox jComboBox;
    JTextField jTextField;
    String[][] obj;//表格数据
    JTable table;
    JScrollPane scrollpane; //表格面板
    String arr[];

    JButton jButton_select;     //查询
    JButton jButton_remove;     //删除
    JButton jButton_update;     //更新
    JButton jButton_add;     //添加
    JButton jButton_all;     //显示全部

    ResultSet resultSet;
    JdbcDeal jdbcDeal;
    int Count;        //获取信息行数

    private void UpdateTable(){
        p2.remove(scrollpane);
        table=new JTable(obj,arr);
        table.setRowHeight(50);
        scrollpane=new JScrollPane(table);
        p2.add(scrollpane,BorderLayout.CENTER);
        p2.revalidate();
        this.revalidate();
    }

    private boolean Table_select(String sql){
        //初始化数据
        try {
            jdbcDeal = new JdbcDeal();
            resultSet = jdbcDeal.select(sql);
            resultSet.last();
            Count = resultSet.getRow();
            resultSet.beforeFirst();
            obj = new String[Count][arr.length];
            int i=0;
            if(!resultSet.next()){
                jdbcDeal.close();
                JOptionPane.showMessageDialog(this, "未查询到相关信息", "", JOptionPane.WARNING_MESSAGE);
                return false;
            }else{
                do{
                    obj[i][0] = resultSet.getString("Wno");
                    obj[i][1] = resultSet.getString("Wname");
                    obj[i][2] = resultSet.getString("Wsex");
                    if(user.isFlag()){
                        obj[i][3] = resultSet.getString("Wpassword");
                    }
                    i++;
                }while (resultSet.next());
                jdbcDeal.close();
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return true;
    }

    public UIworker(User user) {
        this.user = user;

        p1 = new JPanel();
        p1.setOpaque(false);
        Box boxH = Box.createHorizontalBox(); //行式盒
        jButton_all = new JButton("重置");
        JLabel label = new JLabel("请输入查询信息：");
        jTextField = new JTextField(15);
        jComboBox = new JComboBox(new String[]{"","按工号","按姓氏"});
        jButton_select = new JButton("查询");
        jButton_add = new JButton("添加");
        jButton_remove = new JButton("删除");
        jButton_update = new JButton("修改");

        boxH.add(jButton_all);
        boxH.add(Box.createHorizontalStrut(10));
        boxH.add(label);
        boxH.add(jTextField);
        boxH.add(jComboBox);
        boxH.add(jButton_select);
        boxH.add(jButton_add);
        boxH.add(jButton_update);
        boxH.add(jButton_remove);
        p1.add(boxH);

        //判断当前用户是否为管理员
        if(user.isFlag()){
            arr = new String[]{"工 号","姓 名","性 别","密 码"};
        }else {
            arr = new String[]{"工 号","姓 名","性 别"};
        }

        String sql = "SELECT * FROM worker";
        Table_select(sql);

        this.setLayout(new BorderLayout());
        table=new JTable(obj,arr);
        //表格限宽
        table.setRowHeight(50);
        //创建显示表格的滚动面板
        scrollpane=new JScrollPane(table);

        p2 = new JPanel();
        p2.setLayout(new BorderLayout());
        p2.add(scrollpane,BorderLayout.CENTER);
        p2.add(new JButton("*********员工信息**********"),BorderLayout.NORTH);
        p2.add(new JButton("-------------------------"),BorderLayout.SOUTH);
        p2.setOpaque(false);

        if(!user.isFlag()){
            jButton_add.setEnabled(false);
            jButton_update.setEnabled(false);
            jButton_remove.setEnabled(false);
        }

        Box boxV = Box.createVerticalBox();
        boxV.add(p1);
        boxV.add(p2);

        jButton_all.addActionListener(this);
        jButton_select.addActionListener(this);
        jButton_remove.addActionListener(this);
        jButton_update.addActionListener(this);
        jButton_add.addActionListener(this);

        this.setLayout(new BorderLayout());
        this.add(boxV,BorderLayout.CENTER);
        setVisible(true);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String texts = jTextField.getText();
        //下拉框
        String jBox = jComboBox.getSelectedItem().toString();

        if (e.getSource() == jButton_select){
            boolean select_flag = false;
            if(texts.equals("")) {
                JOptionPane.showMessageDialog(this, "请输入查询信息!", "", JOptionPane.WARNING_MESSAGE);
                return;
            }else if(jBox.equals("")){
                JOptionPane.showMessageDialog(this, "请选择查找类型!", "", JOptionPane.WARNING_MESSAGE);
                return;
            }else if(jBox.equals("按姓氏")){
                String sql = "SELECT * FROM worker WHERE Wname like '" + texts + "%'";
                select_flag = Table_select(sql);
            }else if(jBox.equals("按工号")){
                String sql = "SELECT * FROM worker WHERE Wno = '" + texts + "'";
                select_flag = Table_select(sql);
            }
            if(select_flag){
                p2.remove(scrollpane);
                UpdateTable();
                return;
            }


        }else if(e.getSource() == jButton_add){
            Worker_add worker_add = new Worker_add();
            String sql = "SELECT * FROM worker";
            Table_select(sql);
            UpdateTable();
            return;
        }else if(e.getSource() == jButton_all){
            String sql = "SELECT * FROM worker";
            Table_select(sql);
            UpdateTable();
            return;
        }else{
            if(table.getSelectedRows().length>1 || table.getSelectedRow()==-1){
                JOptionPane.showMessageDialog(this, "请选择一行！", "", JOptionPane.WARNING_MESSAGE);
                return;
            }else if(e.getSource() == jButton_remove){
                String table_wno = (String) table.getValueAt(table.getSelectedRow(),0);
                String sql = "DELETE FROM worker WHERE Wno = '" + table_wno + "'";
                try {
                    jdbcDeal = new JdbcDeal();
                    if(jdbcDeal.del(sql) == 1){
                        JOptionPane.showMessageDialog(this, "成功删除一条记录！","",JOptionPane.PLAIN_MESSAGE);
                        sql = "SELECT * FROM worker";
                        Table_select(sql);
                        UpdateTable();
                        return;
                    } else {
                        JOptionPane.showMessageDialog(this, "删除记录失败！","", JOptionPane.WARNING_MESSAGE);
                        return;
                    }
                } catch (SQLException throwables) {
                    JOptionPane.showMessageDialog(this, "删除记录失败！可能和其他信息有联动！请先删除相关信息！","", JOptionPane.WARNING_MESSAGE);
                    throwables.printStackTrace();
                }

            }else if (e.getSource() == jButton_update){
                User update_user = new User();
                update_user.setWorkNo((String)table.getValueAt(table.getSelectedRow(),0));
                update_user.setName((String)table.getValueAt(table.getSelectedRow(),1));
                update_user.setSex((String)table.getValueAt(table.getSelectedRow(),2));
                update_user.setPassword((String)table.getValueAt(table.getSelectedRow(),3));

                Worker_update worker_update = new Worker_update(update_user);

                String sql = "SELECT * FROM worker";
                Table_select(sql);
                UpdateTable();
                return;
            }
        }

    }

}
